// src/api/FlightApi.ts
import { BaseApiClient } from '../BaseApi.ts';
import type { FlightData, ApiResponse } from '../../types/DataTypes.ts';

// 航班相关API
export class FlightApi extends BaseApiClient {
    constructor() {
        super();
    }

    // 获取所有航班
    async getAllFlights(): Promise<ApiResponse<FlightData[]>> {
        return this.request<FlightData[]>({
            method: 'GET',
            url: '/flights',
        });
    }

    // 高级搜索航班（支持多条件组合）
    async advancedSearch(criteria: {
        flightNum?: string;
        fromCity?: string;
        arivCity?: string;
    }): Promise<ApiResponse<FlightData[]>> {
        const params = new URLSearchParams();
        
        if (criteria.flightNum) params.append('flightNum', criteria.flightNum);
        if (criteria.fromCity) params.append('fromCity', criteria.fromCity);
        if (criteria.arivCity) params.append('arivCity', criteria.arivCity);

        return this.request<FlightData[]>({
            method: 'GET',
            url: `/flights/search/advanced?${params.toString()}`,
        });
    }


    // 添加新航班
    async addFlight(flight: Omit<FlightData, 'availSeat'> & { availSeat?: number }): Promise<ApiResponse<FlightData>> {
        return this.request<FlightData>({
            method: 'POST',
            url: '/flights',
            data: {
                ...flight,
                availSeat: flight.availSeat ?? flight.totalSeat, // 默认可用座位等于总数
            },
        });
    }

    // 更新航班信息
    async updateFlight(flightData: FlightData): Promise<ApiResponse> {
        return this.request({
            method: 'PUT',
            url: '/flights/update',
            data: flightData,
        });
    }

    // 删除航班
    async deleteFlight(flightNum: string): Promise<ApiResponse> {
        return this.request({
            method: 'DELETE',
            url: `/flights/${encodeURIComponent(flightNum)}`,
        });
    }

}

// 创建单例实例
export const flightApi = new FlightApi();

// 默认导出
export default flightApi;